package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.TblDevice;
import com.frame.work.entity.TblZigbeeDeviceProperty;
import com.frame.work.entity.dto.LightDeviceDTO;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.TblDeviceMapper;
import com.frame.work.service.impl.TblZigbeeDevicePropertyService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 多彩灯
 */
@Data
public class DuoCaiDeng_0FAA0B02 extends BaseFrame {

    private String switchState;
    private String spreadingType;


    public DuoCaiDeng_0FAA0B02(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据
        this.setSpreadingType(spreadingType(data));

        switch (childIndex(data)) {
            //调光
            case "180007FF":
                this.setSwitchState("01");
                Map<String, Object> map = setBrightness(data);
                this.setActions(map);
                JsonToMqtt.actionHandleNoMqtt(gatewayMac, JSONObject.toJSONString(this));
                LightDeviceDTO lightDeviceDTO = setPropertyFun(data, "brightness");
                map.put("props", lightDeviceDTO);
                JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
                break;
            //调色
            case "780007FF":
                this.setSwitchState("01");
                Map<String, Object> map1 = setColor(data);
                this.setActions(map1);
                JsonToMqtt.actionHandleNoMqtt(gatewayMac, JSONObject.toJSONString(this));
                LightDeviceDTO lightDeviceDTO1 = setPropertyFun(data, "color");
                map1.put("props", lightDeviceDTO1);
                JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
                break;
            //关闭状态
            case "080007FF":
                this.setSwitchState("02");
                this.setActions(setClose(data));
                JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
                break;
            default:
                break;
        }
    }


    /**
     * 子索引选择项
     * 用于不同类型操作的数据上报
     * 180007FF:调光
     * 780007FF:调色
     * 080007FF:关闭
     *
     * @return
     */
    private String childIndex(String data) {
        return StringUtils.substring(data, 26, 34);
    }

    private String spreadingType(String data) {
        return StringUtils.substring(data, 38, 40);
    }

    /**
     * 亮度值
     */
    private static String brightness(String data) {
        return StringUtils.substring(data, data.length() - 2, data.length());
    }

    /**
     * 设置亮度值Map
     */

    private static Map<String, Object> setBrightness(String data) {
        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("controlType", "AIMING_CONTROL");
        actions.put("switchState", "01");
        actions.put("brightness", brightness(data));
        return actions;
    }

    /**
     * 设置颜色值Map
     */
    private static Map<String, Object> setColor(String data) {
        Map<String, Object> actions = new HashMap();
        actions.put("controlType", "COLOR_CONTROL");
        actions.put("switchState", "01");
        actions.put("brightness_r", red(data));
        actions.put("brightness_g", green(data));
        actions.put("brightness_b", blue(data));
        return actions;
    }


    private static Map<String, Object> setClose(String data) {
        Map<String, Object> actions = new HashMap();
        actions.put("controlType", "CLOSE_CONTROL");
        actions.put("switchState", "02");
        return actions;
    }


    /**
     * rgb颜色
     */
    private static String red(String data) {
        return StringUtils.substring(data, data.length() - 6, data.length() - 4);
    }

    /**
     * rgb颜色
     */
    private static String green(String data) {
        return StringUtils.substring(data, data.length() - 4, data.length() - 2);
    }

    /**
     * rgb颜色
     */
    private static String blue(String data) {
        return StringUtils.substring(data, data.length() - 2, data.length());
    }


    /**
     * 设置属性
     *
     * @param data
     * @param type
     */
    private LightDeviceDTO setPropertyFun(String data, String type) {
        LightDeviceDTO lightDevice = new LightDeviceDTO();
        TblDevice device = SpringUtil.getBean(TblDeviceMapper.class).selectOne(Wrappers.<TblDevice>lambdaQuery()
                .eq(TblDevice::getZigbeeMac, this.getDeviceMac()));
        if (device != null) {
            TblZigbeeDevicePropertyService propertyService = SpringUtil.getBean(TblZigbeeDevicePropertyService.class);
            TblZigbeeDeviceProperty property = propertyService.getById(device.getId());
            if (property != null) {
                JSONObject jsonObject = JSONObject.parseObject(property.getProperty());
                lightDevice = JSON.toJavaObject(jsonObject, LightDeviceDTO.class);
                String color = "color";
                if (StringUtils.equals(color, type)) {
                    lightDevice.setRed(red(data));
                    lightDevice.setGreen(green(data));
                    lightDevice.setBlue(blue(data));
                    lightDevice.setColorType(false);
                } else {
                    lightDevice.setWhite(brightness(data));
                    lightDevice.setColorType(true);
                }
                lightDevice.setDuration(jsonObject.get("duration") == null ? 0 : (Integer) jsonObject.get("duration"));
            } else {
                String color = "color";
                if (StringUtils.equals(color, type)) {
                    lightDevice.setRed(red(data));
                    lightDevice.setGreen(green(data));
                    lightDevice.setBlue(blue(data));
                    lightDevice.setWhite("00");
                    lightDevice.setColorType(false);
                } else {
                    lightDevice.setRed("00");
                    lightDevice.setGreen("00");
                    lightDevice.setBlue("00");
                    lightDevice.setWhite(brightness(data));
                    lightDevice.setColorType(true);
                }

            }
            TblZigbeeDeviceProperty zigbeeDeviceProperty = new TblZigbeeDeviceProperty();
            zigbeeDeviceProperty.setProperty(JSONObject.toJSONString(lightDevice));
            zigbeeDeviceProperty.setId(device.getId());
            propertyService.saveOrUpdate(zigbeeDeviceProperty);
        }
        return lightDevice;
    }

}
